/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sysdrive.controller.ContaPagar;

import br.com.sysdrive.DAO.ContaPagarDAOImpl;
import br.com.sysdrive.DAO.GenericDAO;
import static br.com.sysdrive.controller.Compra.CadastrarCompra.converterData;
import br.com.sysdrive.model.Compra;
import br.com.sysdrive.model.ContaPagar;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Willian Frias
 */
public class AlterarContaPagar extends HttpServlet {
public static Date converterData(String data) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
        if (data == null || data.trim().equals("")) {
            return null;
        } else {
            Date date = fmt.parse(data);
            return date;
        }
    }
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        
         Date dataVencimentoContaPagar = converterData(request.getParameter("datavencimentocontapagar"));
         Double valorParcelaContaPagar = Double.parseDouble(request.getParameter("valorparcelacontapagar"));
         Integer descontoContaPagar = Integer.parseInt(request.getParameter("descontocontapagar"));         
         Integer jurosContaPagar = Integer.parseInt(request.getParameter("juroscontapagar"));
         Double valorTotalContaPagar = Double.parseDouble(request.getParameter("valortotalcontapagar"));
         Integer qtdParcelaContaPagar = Integer.parseInt(request.getParameter("qtdparcelacontapagar"));
         Integer numeroParcelaContaPagar = Integer.parseInt(request.getParameter("numeroparcelacontapagar"));
         String statusContaPagar = request.getParameter("statuscontapagar");
         Integer idContaPagar = Integer.parseInt(request.getParameter("idcontapagar"));  
         
          ContaPagar contaPagar = new ContaPagar();
          contaPagar.setDataVencimentoContaPagar(dataVencimentoContaPagar);
          contaPagar.setValorParcelaContaPagar(valorParcelaContaPagar);
          contaPagar.setDescontoContaPagar(descontoContaPagar);
          contaPagar.setJurosContaPagar(jurosContaPagar);
          contaPagar.setValorTotalContaPagar(valorTotalContaPagar);
          contaPagar.setQuantidadeParcelaContaPagar(qtdParcelaContaPagar);
          contaPagar.setNumeroParcelaContaPagar(numeroParcelaContaPagar);
          contaPagar.setStatusContaPagar(statusContaPagar);
          contaPagar.setIdContaPagar(idContaPagar);

        
           String mensagem = "";
           try {
            
               GenericDAO daoContaPagar = new ContaPagarDAOImpl();
            if (daoContaPagar.alterar(contaPagar)) {
                    mensagem = "Conta Pagar Alterada com Sucesso!";;
                } else {
                    mensagem = "Dados inválidos – Verifique dados obrigatórios";
                }
             request.setAttribute("contaspagar", daoContaPagar.listar());
            request.getRequestDispatcher("ListarContaPagar").forward(request, response);
        } catch (Exception ex) {
            System.out.println("Erro ao Alterar Conta Pagar! " + ex.getMessage());
   }


}


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (ParseException ex) {
        Logger.getLogger(AlterarContaPagar.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (ParseException ex) {
        Logger.getLogger(AlterarContaPagar.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
